﻿#define _CRT_SECURE_NO_WARNINGS 1
#include"Snake.h"
  
//函数的声明

//定位光标位置
void SetPos(short x, short y)
{
	HANDLE houtput = GetStdHandle(STD_OUTPUT_HANDLE);//获得标准输出设备的句柄
	COORD pos = { x,y };
	SetConsoleCursorPosition(houtput, pos);//定位光标位置
}
//游戏的初始化
void GameStart(pSnake ps)
{
	//先设置窗口大小，在隐藏光标
	system("mode con cols=100 lines=30");
	system("title 贪吃蛇");
	HANDLE houtput = GetStdHandle(STD_OUTPUT_HANDLE);//获得标准输出设备的句柄
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(houtput, &CursorInfo);//获取光标控制台信息
	CursorInfo.bVisible = false;//隐藏控制台光标
	SetConsoleCursorInfo(houtput, &CursorInfo);
	//定位光标位置
	//1. 打印环境界面和功能介绍
	WelcomeToGame();
	CreateMap();
	InitSnake(ps);
	CreateFood(ps);
	PrintHelpInfo();

}

//欢迎界面的打印
void WelcomeToGame()
{
	SetPos(40, 14);
	wprintf(L"欢迎来到贪吃蛇小游戏\n");
	SetPos(42, 20);
	system("pause");
	system("cls");
	SetPos(25, 14);
	wprintf(L"用 ↑. ↓ . ← . → 来控制蛇的移动，按F3加速，F4减速\n");
	SetPos(25, 15);
	wprintf(L"加速能够得到更高的分数\n");

	SetPos(42, 20);
	system("pause");
	system("cls");
}

//创建地图
void CreateMap()
{
	int i = 0;
	for (i = 0; i < 29; i++)
	{
		wprintf(L"%lc", WALL);
	}//上
	SetPos(0, 26);
	for (i = 0; i < 29; i++)
	{
		wprintf(L"%lc", WALL);
	}//下
	for (i = 1; i <= 25; i++)
	{
		SetPos(0, i);
		wprintf(L"%lc", WALL);
	}//左
	for (i = 1; i <= 25; i++)
	{
		SetPos(56, i);
		wprintf(L"%lc", WALL);
	}//右
}

//初始化蛇身
void InitSnake(pSnake ps)
{
	pSnakeNode cur = NULL;
	int i;
	for(i=0;i<5;i++)
	{
		cur = (pSnakeNode)malloc(sizeof(SnakeNode));
		if (cur == NULL)
		{
			perror("malloc");
		}
		cur->x = POS_X + i *2;
		cur->y = POS_Y;
		cur->next = NULL;
		//头插
		if (ps->_phead == NULL)
		{
			ps->_phead = cur;
		}
		else
		{
			cur->next = ps->_phead;
			ps->_phead = cur;
		}
	}
	//打印蛇身
	cur = ps->_phead;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	ps->_sleeptime = 200;
	ps->_score = 0;
	ps->_sta = OK;
	ps->_dir = RIGHT;
	ps->_foodweight = 10;
}

//创建食物
void CreateFood(pSnake ps)
{
	int x = 0;
	int y = 0;
again:
	//产⽣的x坐标应该是2的倍数，这样才可能和蛇头坐标对⻬。
	do
	{
			x = rand() % 53 + 2;
			y = rand() % 25 + 1;
	} while (x % 2 != 0);
	pSnakeNode cur = ps->_phead;
	//获取指向蛇头的指针

		//⻝物不能和蛇⾝冲突
	while (cur)
	{
		if (cur->x == x && cur->y == y)
		{
				goto again;
		}
		cur = cur->next;
	}
	pSnakeNode pFood = (pSnakeNode)malloc(sizeof(SnakeNode));//创建⻝物

		if (pFood == NULL)
		{
			perror("CreateFood::malloc()");
			return;
		}
		else
		{
			pFood->x = x;
			pFood->y = y;
			SetPos(pFood->x, pFood->y);
			wprintf(L"%c", FOOD);
			ps->_pfood= pFood;
		}
}
void PrintHelpInfo()
{
	SetPos(62, 15);
	printf("1.不能穿墙，不能咬到自己");
	SetPos(62, 16);
	printf("2.用 ↑.↓.←.→ 来控制蛇的移动");
	SetPos(62, 17);
	printf("3.F3是加速，F4是减速");

	SetPos(62, 19);
}

//游戏运行的逻辑
void GameRun(pSnake ps)
{

	do
	{
		//当前的分数情况
		SetPos(62, 10);
		printf("总分:%5d\n", ps->_score);
		SetPos(62, 11);
		printf("食物的分值:%02d\n", ps->_foodweight);
		//检测按键
		//上、下、左、右、ESC、空格、F3、F4
		if (KEY_PRESS(VK_UP) && ps->_dir != DOWN)
		{
			ps->_dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->_dir != UP)
		{
			ps->_dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->_dir != RIGHT)
		{
			ps->_dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->_dir != LEFT)
		{
			ps->_dir = RIGHT;
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			ps->_sta = END_NORMAL;
			break;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			//游戏要暂定
			//pause();//暂定和回复暂定
		}
		else if (KEY_PRESS(VK_F3))
		{
			if (ps->_sleeptime >= 80)
			{
				ps->_sleeptime -= 30;
				ps->_foodweight += 2;
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			if (ps->_foodweight > 2)
			{
				ps->_sleeptime += 30;
				ps->_foodweight -= 2;
			}
		}
		//走一步
		SnakeMove(ps);

		////睡眠一下
		Sleep(ps->_sleeptime);

	} while (ps->_sta == OK);
	system("cls");
}

//蛇的移动-走一步
void SnakeMove(pSnake ps)
{
	pSnakeNode pNext = (pSnakeNode)malloc(sizeof(SnakeNode));
	if (pNext == NULL)
	{
		perror("SnakeMove()::malloc()");
		return;
	}
	pNext->next = NULL;

	switch (ps->_dir)
	{
	case UP:
		pNext->x = ps->_phead->x;
		pNext->y = ps->_phead->y - 1;
		break;
	case DOWN:
		pNext->x = ps->_phead->x;
		pNext->y = ps->_phead->y + 1;
		break;
	case LEFT:
		pNext->x = ps->_phead->x - 2;
		pNext->y = ps->_phead->y;
		break;
	case RIGHT:
		pNext->x = ps->_phead->x + 2;
		pNext->y = ps->_phead->y;
		break;
	}

	//下一个坐标处是否是食物
	if (NextIsFood(ps, pNext))
	{
		//是食物就吃掉
		EatFood(ps, pNext);
	}
	else
	{
		//不是食物就正常一步
		NoFood(ps, pNext);
	}

	//检测撞墙
	KillByWall(ps);

	//检测撞到自己
	KillBySelf(ps);
}


int NextIsFood(pSnake ps, pSnakeNode pNext)
{
	if (ps->_pfood->x == pNext->x && ps->_pfood->y == pNext->y)
		return 1;//下一个坐标处是食物
	else
		return 0;
}

void EatFood(pSnake ps, pSnakeNode pNext)
{
	pNext->next = ps->_phead;
	ps->_phead = pNext;

	//打印蛇
	pSnakeNode cur = ps->_phead;
	while (cur)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}

	ps->_score+= ps->_foodweight;

	//新建食物
	CreateFood(ps);
}

void NoFood(pSnake ps, pSnakeNode pNext)
{
	//头插法
	pNext->next = ps->_phead;
	ps->_phead = pNext;

	//释放尾结点
	pSnakeNode cur = ps->_phead;
	while (cur->next->next)
	{
		SetPos(cur->x, cur->y);
		wprintf(L"%lc", BODY);
		cur = cur->next;
	}
	//将尾节点的位置打印成空白字符
	SetPos(cur->next->x, cur->next->y);
	printf("  ");

	free(cur->next);
	cur->next = NULL;//易错
}


//检测是否撞墙
void KillByWall(pSnake ps)
{
	if (ps->_phead->x == 0 ||
		ps->_phead->x == 56 ||
		ps->_phead->y == 0 ||
		ps->_phead->y == 26)
	{
		ps->_sta = KILL_BY_WALL;
	}
}

//检测是否撞自己
void KillBySelf(pSnake ps)
{
	pSnakeNode cur = ps->_phead->next;//从第二个节点开始
	while (cur)
	{
		if (cur->x == ps->_phead->x && cur->y == ps->_phead->y)
		{
			ps->_sta = KILL_BY_SELF;
			return;
		}
		cur = cur->next;
	}
}


//游戏善后的工作
int GameEnd(pSnake ps)
{
	SetPos(15, 12);
	switch (ps->_sta)
	{
	case END_NORMAL:
		printf("主动退出游戏，正常退出\n");
		break;
	case KILL_BY_WALL:
		printf("很遗憾，撞墙了，游戏结束\n");
		break;
	case KILL_BY_SELF:
		printf("很遗憾，咬到自己了，游戏结束\n");
		break;
	}
	int score = ps->_score;
	//释放贪吃蛇的链表资源
	pSnakeNode cur = ps->_phead;
	pSnakeNode tmp = NULL;

	while (cur)
	{
		tmp = cur;
		cur = cur->next;
		free(tmp);
	}
	free(ps->_pfood);
	return score;
}